package de.baumann.browser.activitys.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View

import com.umeng.analytics.MobclickAgent

import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.ILoginView
import de.baumann.browser.present.LoginPresenter
import de.baumann.browser.utils.Constants
import kotlinx.android.synthetic.main.activity_login.*

/**
 * @author：
 * @date：2018/9/18
 * @discription:用户登录
 */
class LoginActivity : BaseOdinActivity<ILoginView, LoginPresenter>(), ILoginView {

    companion object {
        fun startLoginActivity(context: Activity) {
            context.startActivityForResult(Intent(context, LoginActivity::class.java), Constants.LOGINCODE)
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun getLoadingText(): String {
        return getString(R.string.logging_in)
    }

    override fun initView() {
        btnLogin.setOnClickListener {
            presenter.login()
        }

        tvRegister.setOnClickListener {
            startActivity(Intent(mContext, RegisterActivity::class.java))
        }

        tvRetrievePassword.setOnClickListener {
            startActivity(Intent(mContext, RetrievePasswordActivity::class.java))
        }

        ivBack.setOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            finish()
        }
    }

    override fun initData() {}

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override fun createView(): ILoginView {
        return this
    }

    override fun getAccount(): String {
        return edLoginAccount.text.trim().toString()
    }

    override fun getPassword(): String {
        return edLoginPassword.text.trim().toString()
    }

    override fun loginSuccess(loginName: String, licenceStatus: Int) {
        showShortToast(getString(R.string.login_successful))
        MobclickAgent.onProfileSignIn(loginName)
        val intent = Intent()
        intent.putExtra("licenceStatus", licenceStatus)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun showBindEth(): Boolean {
        return false
    }
}
